-- NativeUIDemoHighLevel.lua
-- Author: Mikael Kindborg
-- Date: 2011-02-22
-- Description: Demo of NativeUI in Lua that uses
-- function closures for widget events.

-- Set a table field to a value if the field is nil.
-- This is useful as a shorthand when setting widget
-- property default values.
function SetIfNil(table, field, value)
  if nil == table[field] then
    table[field] = value
  end
end

-- Table of widget event handler functions.
-- This table maps widget ids to functions.
WidgetEventHandlers = {}

-- Here we handle widget events and dispatch to
-- the registered widget event function.
System.onCustomEvent(function(event)
  if EVENT_TYPE_WIDGET == maEventGetType(event) then
    -- Get the widget event data structure of the event.
    local widgetEvent = maEventGetData(event)
    -- The widget handle is the second int in the struct.
    local widget = maBufferGetInt(widgetEvent, 1)
    -- Do we have an event function for this widget?
    local eventFun = WidgetEventHandlers[widget]
    if nil ~= eventFun then
      -- Yes we have, call the function.
      eventFun(widgetEvent)
    end
  end
end)

-- Widget size constants.
FILL_PARENT = "-1"
WRAP_CONTENT = "-2"

-- Function that creates a widget.
-- Property names are properties available for
-- maWidgetSetProperty, plus "type", "parent" and "eventFun".
function CreateWidget(properties)
  -- Create the widget.
  local widget = maWidgetCreate(properties.type)
  if WIDGET_ERROR == widget then
    return nil
  end
  -- Set properties. Properties "parent", "type", and
  -- "eventFun" are handled as special cases.
  for prop,value in pairs(properties) do
    if "parent" == prop then
      maWidgetAddChild(value, widget)
    elseif "eventFun" == prop then
      -- Add function as event handler for this widget.
      WidgetEventHandlers[widget] = value
    elseif "type" ~= prop then
      maWidgetSetProperty(widget, prop, value)
    end
  end
  return widget
end

-- Function that creates a button widget.
function CreateButton(properties)
  properties.type = "Button"
  SetIfNil(properties, "width", FILL_PARENT)
  SetIfNil(properties, "height", WRAP_CONTENT)
  SetIfNil(properties, "textHorizontalAlignment", "center")
  SetIfNil(properties, "textVerticalAlignment", "center")
  SetIfNil(properties, "fontSize", "20.0")
  SetIfNil(properties, "fontColor", "000000")
  return CreateWidget(properties)
end
 
-- Create first screen.

Screen1 = CreateWidget { type = "Screen" }
maWidgetScreenShow(Screen1)

MainLayout1 = CreateWidget 
{
  type = "VerticalLayout",
  parent = Screen1,
  width = FILL_PARENT,
  height = FILL_PARENT,
  backgroundColor = "FF8800" 
}

MessageLabel =  CreateWidget 
{
  type = "Label",
  parent = MainLayout1,
  width = FILL_PARENT,
  height = WRAP_CONTENT, 
  fontSize = "26.0",
  fontColor = "FFFFFF",
  text = "Demo of MoSync NativeUI" 
}

ButtonSayHello = CreateButton 
{ 
  parent = MainLayout1,
  text = "Say Hello",
  eventFun = function(widgetEvent)
    maWidgetSetProperty(MessageLabel, "text", "Hello World!")
  end
}

ButtonSayHi = CreateButton 
{ 
  parent = MainLayout1,
  text = "Say Hi",
  eventFun = function(widgetEvent)
    maWidgetSetProperty(MessageLabel, "text", "Hi there!")
  end
}
  
ButtonOpenWebView = CreateButton 
{ 
  parent = MainLayout1,
  text = "Open WebView",
  eventFun = function(widgetEvent)
    maWidgetScreenShow(Screen2)
  end
}

-- Create second screen.

Screen2 = CreateWidget { type = "Screen" }

MainLayout2 = CreateWidget 
{
  type = "VerticalLayout",
  parent = Screen2,
  width = FILL_PARENT,
  height = FILL_PARENT 
}

ButtonGoBack = CreateButton 
{ 
  parent = MainLayout2,
  text = "Go Back",
  eventFun = function(widgetEvent)
    maWidgetScreenShow(Screen1)
  end
}

WebView = CreateWidget 
{
  type = "WebView",
  parent = MainLayout2,
  width = FILL_PARENT,
  height = FILL_PARENT,
  url = "http://www.mosync.com" 
}

-- Things to try interactively.
-- maWidgetRemoveChild(MainLayout1, ButtonSayHi)
-- maExit(0)
